home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZPAINT.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  10KB  |  344 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zpaint.c */
  20. /* Painting operators for Ghostscript */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "alloc.h"
  25. #include "estack.h"            /* for image[mask] */
  26. #include "ilevel.h"
  27. #include "store.h"
  28. #include "gscspace.h"
  29. #include "gsmatrix.h"
  30. #include "gsimage.h"
  31. #include "gspaint.h"
  32. #include "state.h"
  33. #include "stream.h"
  34.  
  35. /* Forward references */
  36. /* zimage_setup is used by zimage2.c */
  37. int zimage_setup(P10(int width, int height, gs_matrix *pmat,
  38.   ref *sources, int bits_per_component,
  39.   int spread, const gs_color_space *pcs, int masked,
  40.   const float *decode, int npop));
  41. private int image_opaque_setup(P4(os_ptr, int, const gs_color_space_type *, int));
  42. private int image_setup(P7(os_ptr, int, int, const gs_color_space_type *, int, const float *, int));
  43. private int image_read(P2(ref *, ref *));
  44. private int image_continue(P1(os_ptr));
  45. private int image_cleanup(P1(os_ptr));
  46.  
  47. /* - erasepage - */
  48. int
  49. zerasepage(register os_ptr op)
  50. {    return gs_erasepage(igs);
  51. }
  52.  
  53. /* - fill - */
  54. int
  55. zfill(register os_ptr op)
  56. {    return gs_fill(igs);
  57. }
  58.  
  59. /* - eofill - */
  60. int
  61. zeofill(register os_ptr op)
  62. {    return gs_eofill(igs);
  63. }
  64.  
  65. /* - stroke - */
  66. int
  67. zstroke(register os_ptr op)
  68. {    return gs_stroke(igs);
  69. }
  70.  
  71. /* Standard decoding maps for images. */
  72. static const float decode_01[8] = { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
  73. static const float decode_10[8] = { 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 };
  74.  
  75. /* <width> <height> <bits/comp> <matrix> */
  76. /*    true <datasrc_0> ... <datasrc_ncomp-1> <ncomp> colorimage - */
  77. /*    false <datasrc> <ncomp> colorimage - */
  78. int
  79. zcolorimage(register os_ptr op)
  80. {    int spp;            /* samples per pixel */
  81.     int npop = 7;
  82.     os_ptr procp = op - 2;
  83.     static const gs_color_space_type *apcst[] = {
  84.         NULL, &gs_color_space_type_DeviceGray, NULL,
  85.         &gs_color_space_type_DeviceRGB,
  86.         &gs_color_space_type_DeviceCMYK
  87.     };
  88.     int spread = 0;
  89.     check_type(*op, t_integer);    /* ncolors */
  90.     check_type(op[-1], t_boolean);    /* multiproc */
  91.     if ( (ulong)(op->value.intval) > 4 )
  92.         return_error(e_rangecheck);
  93.     switch ( (spp = (int)(op->value.intval)) )
  94.     {
  95.     case 1:
  96.         break;
  97.     case 3: case 4:
  98.         if ( op[-1].value.index )    /* planar format */
  99.             npop += spp - 1,
  100.             procp -= spp - 1,
  101.             spread = 1;
  102.         break;
  103.     default:
  104.         return_error(e_rangecheck);
  105.     }
  106.     return image_opaque_setup(procp, spread, apcst[spp], npop);
  107. }
  108.  
  109. /* <width> <height> <bits/sample> <matrix> <datasrc> image - */
  110. int
  111. zimage(register os_ptr op)
  112. {    return image_opaque_setup(op, 0, &gs_color_space_type_DeviceGray, 5);
  113. }
  114.  
  115. /* <width> <height> <paint_1s> <matrix> <datasrc> imagemask - */
  116. int
  117. zimagemask(register os_ptr op)
  118. {    check_type(op[-2], t_boolean);
  119.     return image_setup(op, 1, 0, &gs_color_space_type_DeviceGray, 1,
  120.                (op[-2].value.index ? decode_01 : decode_10), 5);
  121. }
  122.  
  123. /* Common setup for image and colorimage. */
  124. private int
  125. image_opaque_setup(register os_ptr op, int spread,
  126.   const gs_color_space_type *pcst, int npop)
  127. {    check_type(op[-2], t_integer);    /* bits/sample */
  128.     if ( (ulong)(op[-2].value.intval) > 8 )
  129.         return_error(e_rangecheck);
  130.     return image_setup(op, (int)op[-2].value.intval, spread, pcst, 0, decode_01, npop);
  131. }
  132.  
  133. /* Common setup for [color]image and imagemask. */
  134. private int
  135. image_setup(register os_ptr op, int bps, int spread,
  136.   const gs_color_space_type *pcst, int masked, const float *decode, int npop)
  137. {    gs_matrix mat;
  138.     int code;
  139.     gs_color_space cs;
  140.     check_type(op[-4], t_integer);    /* width */
  141.     check_type(op[-3], t_integer);    /* height */
  142.     if ( op[-4].value.intval <= 0 || op[-3].value.intval < 0 )
  143.         return_error(e_rangecheck);
  144.     if ( (code = read_matrix(op - 1, &mat)) < 0 )
  145.         return code;
  146.     cs.type = pcst;
  147.     return zimage_setup((int)op[-4].value.intval,
  148.                 (int)op[-3].value.intval,
  149.                 &mat, op, bps, spread, &cs, masked, decode, npop);
  150. }
  151.  
  152. /* Common setup for Level 1 image/imagemask/colorimage and */
  153. /* the Level 2 dictionary form of image/imagemask. */
  154. int
  155. zimage_setup(int width, int height, gs_matrix *pmat,
  156.   ref *sources, int bits_per_component,
  157.   int spread, const gs_color_space *pcs,
  158.   int masked, const float *decode, int npop)
  159. {    int code;
  160.     gs_image_enum *penum;
  161.     int px;
  162.     ref *pp;
  163.     int num_sources = (spread ? pcs->type->num_components : 1);
  164.     ref source1;
  165.     /* We push on the estack: */
  166.     /*    Control mark, 4 procs, last plane index, */
  167.     /*    enumeration structure (as bytes). */
  168. #define inumpush 7
  169.     check_estack(inumpush + 2);    /* stuff above, + continuation + proc */
  170.     /* Note that the "procedures" might not be procedures, */
  171.     /* but might be strings or files (Level 2 only). */
  172.     for ( px = 0, pp = sources; px < num_sources; px++, pp++ )
  173.     {    switch ( r_type(pp) )
  174.         {
  175.         case t_file:
  176.             if ( !level2_enabled )
  177.                 return_error(e_typecheck);
  178.         case t_string:
  179.             check_read(*pp);
  180.             break;
  181.         default:
  182.             check_proc(*pp);
  183.         }
  184.     }
  185.     if ( height == 0 )
  186.         return 0;    /* empty image */
  187.     if ( masked )
  188.     {    /* Make sure decode is 0..1 or 1..0 */
  189.         if ( decode[0] == 0.0 && decode[1] == 1.0 )
  190.             masked = 1;
  191.         else if ( decode[0] == 1.0 && decode[1] == 0.0 )
  192.             masked = -1;
  193.         else
  194.             return_error(e_rangecheck);
  195.     }
  196.     if ( (penum = (gs_image_enum *)alloc(1, gs_image_enum_sizeof, "image_setup")) == 0 )
  197.         return_error(e_VMerror);
  198.     code = (masked != 0 ?
  199.         gs_imagemask_init(penum, igs, width, height,
  200.                   masked < 0, pmat, 1) :
  201.         gs_image_init(penum, igs, width, height, bits_per_component,
  202.                   spread, pcs, decode, pmat) );
  203.     if ( code < 0 )
  204.     {    alloc_free((char *)penum,
  205.                1, gs_image_enum_sizeof, "image_setup");
  206.         return code;
  207.     }
  208.     mark_estack(es_other, image_cleanup);
  209.     ++esp;
  210.     for ( px = 0, pp = sources; px < 4; esp++, px++, pp++ )
  211.       if ( px < num_sources )
  212.         *esp = *pp;
  213.       else
  214.         make_null(esp);
  215.     make_int(esp, 0);        /* current plane */
  216.     r_set_size(esp, num_sources);
  217.     ++esp;
  218.     make_tasv(esp, t_string, 0, gs_image_enum_sizeof, bytes, (byte *)penum);
  219.     code = image_read(sources, &source1);
  220.     switch ( code )
  221.     {
  222.     case 1:            /* string or file */
  223.         pop(npop - 1);
  224.         *osp = source1;
  225.         return image_continue(osp);
  226.     case 0:            /* procedure */
  227.         pop(npop);
  228.         return o_push_estack;
  229.     default:
  230.         return code;
  231.     }
  232. }
  233. /* Continuation procedure.  Hand the string to the enumerator. */
  234. private int
  235. image_continue(register os_ptr op)
  236. {    gs_image_enum *penum = (gs_image_enum *)esp->value.bytes;
  237.     int code;
  238.     if ( !r_has_type(op, t_string) )
  239.        {    /* Procedure didn't return a string.  Quit. */
  240.         esp -= inumpush;
  241.         image_cleanup(op);
  242.         return_error(e_typecheck);
  243.        }
  244. top:    code = gs_image_next(penum, op->value.bytes, r_size(op));
  245.     if ( r_size(op) == 0 || code != 0 )    /* stop now */
  246.        {    esp -= inumpush;
  247.         image_cleanup(op);
  248.         if ( code < 0 ) return code;
  249.         code = o_pop_estack;
  250.        }
  251.     else
  252.        {    int px = (int)++(esp[-1].value.intval);
  253.         es_ptr pproc = esp - 5;
  254.         if ( px == r_size(esp - 1) )
  255.             esp[-1].value.intval = px = 0;
  256.         code = image_read(pproc + px, op);
  257.         switch ( code )
  258.         {
  259.         case 1:            /* string or file */
  260.             goto top;
  261.         case 0:            /* procedure */
  262.             code = o_push_estack;
  263.             break;
  264.         default:
  265.             return code;
  266.         }
  267.        }
  268.     pop(1);
  269.     return code;
  270. }
  271. /* Prepare to read from an image data source. */
  272. /* Return 1 for a string (and store the string at *psrc), */
  273. /* 0 for a procedure (push on the estack), <0 for error. */
  274. private int
  275. image_read(ref *psref, ref *psrc)
  276. {    switch ( r_type(psref) )
  277.     {
  278.     case t_string:
  279.         *psrc = *psref;
  280.         return 1;
  281.     case t_file:
  282.     {    stream *s = psref->value.pfile;
  283.         int avail;
  284.         while ( (avail = sbufavailable(s)) == 0 )
  285.         {    int next = sgetc(s);
  286.             if ( next == EOFC )        /* end of data, */
  287.                         /* break with avail == 0 */
  288.                 break;
  289.             if ( next == ERRC )
  290.                 return_error(e_ioerror);
  291.             sputback(s);
  292.         }
  293.         make_string(psrc, a_readonly, avail, sbufptr(s));
  294.         sskip(s, avail);
  295.     }    return 1;
  296.     default:
  297.         push_op_estack(image_continue);
  298.         *++esp = *psref;
  299.         return 0;
  300.     }
  301. }
  302. /* Clean up after enumerating an image */
  303. private int
  304. image_cleanup(os_ptr op)
  305. {    gs_image_enum *penum = (gs_image_enum *)esp[inumpush].value.bytes;
  306.     gs_image_cleanup(penum);
  307.     alloc_free((char *)penum, 1, gs_image_enum_sizeof, "image_cleanup");
  308.     return 0;
  309. }
  310.  
  311. /* ------ Ghostscript-specific operators ------ */
  312.  
  313. /* <width> <height> <data> .imagepath - */
  314. int
  315. zimagepath(register os_ptr op)
  316. {    int code;
  317.     check_type(op[-2], t_integer);
  318.     check_type(op[-1], t_integer);
  319.     check_read_type(*op, t_string);
  320.     if ( r_size(op) < ((op[-2].value.intval + 7) >> 3) * op[-1].value.intval )
  321.         return_error(e_rangecheck);
  322.     code = gs_imagepath(igs,
  323.         (int)op[-2].value.intval, (int)op[-1].value.intval,
  324.         op->value.const_bytes);
  325.     if ( code == 0 ) pop(3);
  326.     return code;
  327. }
  328.  
  329. /* ------ Initialization procedure ------ */
  330.  
  331. op_def zpaint_op_defs[] = {
  332.     {"0eofill", zeofill},
  333.     {"0erasepage", zerasepage},
  334.     {"0fill", zfill},
  335.     {"7colorimage", zcolorimage},
  336.     {"5image", zimage},
  337.     {"5imagemask", zimagemask},
  338.     {"3.imagepath", zimagepath},
  339.     {"0stroke", zstroke},
  340.         /* Internal operators */
  341.     {"0%image_continue", image_continue},
  342.     op_def_end(0)
  343. };
  344.